﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiseLinkWPF.Windows.Controls.GridView;
using WiseLinkWPF.Windows.Controls;
using CCF20Model;
using CCF20.ZICModel;
using System.Collections;
namespace CCF20
{
    /// <summary>
    /// Interaction logic for VehicleMessage.xaml
    /// </summary>
    public partial class CustomerCare : UserControl
    {
        VehicleModel vehicleModel;
        CCF20Model.ZIC.ZicWebService zicSvc;

        public CustomerCare()
        {
            InitializeComponent();
            vehicleModel = new VehicleModel();
            zicSvc = ZICMonitorModel.ZICSvc;
        }


        private void VehicleMessageToolBar_Click(object sender, RoutedEventArgs e)
        {
            string strAction = ((Button)sender).Name;

            if (strAction == "Refresh")
            {
                LoadData();
                this.Dispatcher.BeginInvoke(new Action(() => this.gridVehicle.Rebind()));
                return;

            }
            else if (this.gridVehicle.SelectedItem == null)
            {
                return;
            }
            Vehicle currentVehicle = ((VehicleData)this.gridVehicle.SelectedItem).MyVehicle;

            if (currentVehicle.LastContactResult != null &&
                currentVehicle.LastContactResult.Contains("放弃") &&
                strAction != "Restore")
            {
                MessageBox.Show("客户处于放弃状态，请先恢复客户");
                return;
            }
            currentVehicle.Operator = (string)Application.Current.Properties["CurrentUser"];
            ContactHistory contactHistory;
            switch (strAction)
            {
                case "Contact":
                    if ( (currentVehicle.LastContactResult != null ) &&
                          currentVehicle.LastContactResult.Contains("实际回场"))
                    {
                        MessageBox.Show("客户处于实际回场状态，不用联系客户");
                        return;
                    }
                    ContactCustomer contactCustomer = new ContactCustomer(vehicleModel, currentVehicle);
                    contactCustomer.contactList = (IList<ContactHistory>)this.gridContactHistory.ItemsSource;
                    contactCustomer.smsList = (IList<SMSHistory>)this.gridSMSHistory.ItemsSource;
                    contactCustomer.ShowDialog();
                    LoadData();
                    break;
                case "Map":
                    //siteModel model1 = new siteModel();
                    //model1.Latitude = "38.01479333330000";
                    //model1.Longitude = "116.78740500000000";
                    if (currentVehicle.Devices.First().IsSpecialIDC != true)
                    {
                        if (currentVehicle.IsUrgent &&　
                            DateTime.Now - currentVehicle.LatestActiveTime > new TimeSpan(1, 0, 0))
                        {
                            LoggerHelper.LogCCF20(string.Format("地图请求 VehicleID={0} DeviceID={1}, VehcileLastActiveTime={3}",
                                                                currentVehicle.VehicleID,
                                                                currentVehicle.Devices.First().DeviceID,
                                                                currentVehicle.LatestActiveTime));
                        
                        }else
                        {
                            MessageBox.Show("非特殊IDC的车辆，只有在有救援请求,并且请求时间<1小时的情况下，才可以使用地图服务");
                            return;
                        }
                    }
                    string ret;

                    if (ZICMonitorModel.IsJava)
                    {
                        ret = ZICMonitorModel.ZICJava.QuerySite(
                                        ZICMonitorModel.ZICLoginInfo.IDSName,
                                        CCF20Model.ZICMonitorModel.ZICLoginInfo.IDSPassword,
                                        ((VehicleData)this.gridVehicle.SelectedItem).MyDevice.IDCSimNumber);
                    }
                    else
                    {
                        ret = ZICMonitorModel.ZICSvc.QuerySite(
                                        ZICMonitorModel.ZICLoginInfo.IDSName,
                                        CCF20Model.ZICMonitorModel.ZICLoginInfo.IDSPassword,
                                        ((VehicleData)this.gridVehicle.SelectedItem).MyDevice.IDCSimNumber);
                    }
            
                    if (ret == null)
                    {
                        MessageBox.Show("ZIC 地址请求返回为空，请联系智信通");
                        return;
                    }
                    try
                    {
                        siteModel model = (siteModel)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(ret, typeof(siteModel));

                        MessageBoxResult confirmResult = MessageBox.Show(
                                string.Format("经度={0}, 维度={1}", model.Latitude, model.Longitude), "经度,维度正确吗？", MessageBoxButton.OKCancel);

                        if (confirmResult != MessageBoxResult.OK)
                        {
                            return;
                        }

                        HelpMap map = new HelpMap(model);
                        map.ShowDialog();
                    
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }


                    break;
                case "NonUrgent":
                    vehicleModel.RemoveUrgent(currentVehicle);
                    contactHistory = new ContactHistory();
                    contactHistory.ContactResult = "客户实际回场";
                    contactHistory.IsActive = true;
                    contactHistory.ContactTime = DateTime.Now;
                    contactHistory.Vehicle = currentVehicle;
                    contactHistory.Operator = currentVehicle.Operator;
                    vehicleModel.AddContactHistoryForVehicle(currentVehicle, contactHistory);
                  
                    LoadData();
                    break;
                
                case "Restore":
                    if (currentVehicle.LastContactResult != "放弃客户")
                    {
                        MessageBox.Show("不是放弃客户,不可以恢复");
                        return;
                    }
                    
                    vehicleModel.RestoreCustomer(currentVehicle);
                    break;
                case "GiveUp":
                    if (currentVehicle.LastContactResult == "客户实际回场")
                    {
                        MessageBox.Show("客户已经回场,不可以放弃客户");
                        return;
                    }
                    MessageBoxResult confirmGiveup = MessageBox.Show(
                             string.Format("请确认放弃客户-", currentVehicle.Customer.Name), "CCF2.0", MessageBoxButton.OKCancel);
                    if (confirmGiveup != MessageBoxResult.OK)
                        {
                            return;
                        }
                    vehicleModel.GiveUpCustomer(currentVehicle);
                    break;
                case "Return": //客户回场

                    if (currentVehicle.LastContactResult == "客户实际回场")
                    {
                        MessageBox.Show("客户已经回场");
                        return;
                    }
                    currentVehicle.LastContactResult = "客户实际回场";
                    vehicleModel.VehicleRepairStart(currentVehicle,DateTime.Now);
                    contactHistory = new ContactHistory();
                    contactHistory.ContactResult = "客户实际回场";
                    contactHistory.IsActive = true;
                    contactHistory.ContactTime = DateTime.Now;
                    contactHistory.Vehicle = currentVehicle;
                    contactHistory.Operator = currentVehicle.Operator;
                    vehicleModel.AddContactHistoryForVehicle(currentVehicle, contactHistory);
                    ((IList<ContactHistory>)this.gridContactHistory.ItemsSource).Add(contactHistory);
                    break;
                case "Leave": //客户出场
                    if (currentVehicle.LastContactResult != "客户实际回场")
                    {
                        MessageBox.Show("客户还未回场，不可以出场");
                        return;
                    }
                    RepairEnd repairEnd = new RepairEnd(currentVehicle, vehicleModel);
                    repairEnd.ShowDialog();
                    LoadData();
                    break;
            }
        }
        private void LoadData()
        {
            IList<VehicleData> mySource = vehicleModel.GetAllVehicles(false).ToList();
            this.gridVehicle.ItemsSource = mySource;
            //this.gridVehicle.ItemsSource = mySource.Where(v => v.MyVehicle.ConfirmedFaultCount > 0
            //                                           || v.MyVehicle.UrgentCount > 0
            //                                           || v.MyVehicle.MaintenanceCount > 0);
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {


            LoadData();

        }

        private void gridVehicle_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (e.AddedItems.FirstOrDefault() != null)
            {
                this.gridVehicle.CurrentItem = e.AddedItems.First();
                VehicleData vd = (VehicleData)this.gridVehicle.CurrentItem;
                this.gridFault.ItemsSource = vehicleModel.GetConfirmedFaultByVehicle(vd.MyVehicle.VehicleID);
                this.gridMaintenance.ItemsSource = vehicleModel.GetVehicleMaintenanceRequestByVehicle(vd.MyVehicle.VehicleID, true);
                this.gridContactHistory.ItemsSource = vehicleModel.GetContactHistoryByVehicle(vd.MyVehicle.VehicleID, true);
                this.gridSMSHistory.ItemsSource = vehicleModel.GetSMSHistoryByVehicle(vd.MyVehicle.VehicleID, true);
                

            }
            else
            {
                this.gridFault.ItemsSource = null;
                this.gridContactHistory.ItemsSource = null;
                this.gridSMSHistory.ItemsSource = null;
                this.gridMaintenance.ItemsSource = null;

            }
            this.Dispatcher.BeginInvoke(new Action(() => this.gridFault.Rebind()));
            this.Dispatcher.BeginInvoke(new Action(() => this.gridContactHistory.Rebind()));
            this.Dispatcher.BeginInvoke(new Action(() => this.gridMaintenance.Rebind()));
            this.Dispatcher.BeginInvoke(new Action(() => this.gridSMSHistory.Rebind()));

        }




    }
}
